/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* MathUtil.java
*
* Created on November 22, 2007, 10:36 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
 */
package javautil.lang;

import javautil.collections.DimensionCheck;
import javautil.io.LogUtil;

import java.util.List;

/**
 *
 * @author stewari1
 */
public class MathUtil {

    public static void main(String[] args) {
        LogUtil.print(sum(new int[] { 1, 2, 3, 4, 5 }, new int[] { 1, 2, 3, 4, 5 }));
    }

    public static int Sum(int[] array) {

        int result = 0;

        for (int index = 0; index < array.length; index++) {

            result += array[index];
        }

        return result;
    }

    public static int[] sum(int[]... arrays) {

        if (!DimensionCheck.isSameDimen(arrays)) {
            throw new Error("Dimensions are not the same");
        }

        int[] result = new int[arrays[0].length];

        for (int i = 0; i < result.length; i++) {

            for (int j = 0; j < arrays.length; j++) {

                result[i] += arrays[j][i];
            }
        }

        return result;
    }

    public static int[] cumulative(int[] array) {

        int[] cumuArray = new int[array.length];
        int prevTotal   = array[0];

        cumuArray[0] = array[0];

        for (int i = 1; i < array.length; i++) {

            cumuArray[i] = prevTotal + array[i];
            prevTotal    += array[i];
        }

        return cumuArray;
    }

    public static int Sum(List<Integer> list) {

        int result = 0;

        for (Integer intObj : list) {

            result += intObj.intValue();
        }

        return result;
    }

    public static double Sum(double[] array) {

        double result = 0.0;

        for (int index = 0; index < array.length; index++) {

            result += array[index];
        }

        return result;
    }

    /**
     * counts the number of <code<true</code>
     * @param array
     * @return
     */
    public static int Sum(boolean[] array) {

        int result = 0;

        for (boolean val : array) {

            if (val) result++;
        }

        return result;
    }

    public static boolean contains(int val, int val1, int val2) {
        return (val >= val1) && (val <= val2);
    }
}
